//
//  NetworkTool.m
//  Wedding fantastic
//
//  Created by dllo on 15/6/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "NetworkTool.h"

@implementation NetworkTool

/**
 *  网络请求
 *
 *  @param url     请求url
 *  @param body    网络请求携带的Body
 *  @param style   请求返回的数据的格式
 *  @param success 成功调用的Block
 *  @param failure 失败调用的Block
 */

+ (void)get:(NSString *)url
       body:(id)body
 httpResult:(HTTPStyle)style
    success:(void(^)(id result))success
    failure:(void(^)(NSError *error))failure {
    // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // 2.判断返回的responseObject的数据类型
    switch (style) {
        // NSData
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        // JSON类型
        case JSON:
            break;
        // XML类型
        case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        default:
            break;
    }
    
    // 3.让管理类调用get请求
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果成功返回数据
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败返回error信息
        if (failure) {
            failure(error);
        }
    }];
}

/**
 *  网络请求Post
 *
 *  @param url       请求的url
 *  @param body      请求需携带的body的内容
 *  @param bodyStyle 网络请求返回的数据类型
 *  @param style     网络请求body的类型
 *  @param success   成功调用的block
 *  @param failure   失败调用的Block
 */

+ (void)post:(NSString *)url
        body:(id)body
  httpResult:(RequestBodyStyle)bodyStyle
  httpResult:(HTTPStyle)style
     success:(void(^)(id result))success
     failure:(void(^)(NSError *error))failure {
    // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    // 2.判断返回的requestObject的数据类型
    switch (bodyStyle) {
        case BodyHTTP:
            manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            break;
        case BodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
        default:
            break;
    }
    
    // 3.判断返回的responseObject的数据类型
    switch (style) {
        // NSData
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        // JSON类型
        case JSON:
            break;
        // XML类型
        case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        default:
            break;
    }

    //  4.让管理类调用post请求
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果成功返回数据
        if (success) {
            success(responseObject);
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败返回error信息
        if (failure) {
            failure(error);
        }
    }];
}

@end
